package gui;

import net.BackClient;
import net.ReceiveInformation;
import util.Protocol;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class FriendListUi extends JFrame {

    String nickname;
    String friendNickname;
    JLabel[] list = new JLabel[10];
    String[] friendID;
    ReceiveInformation information = new ReceiveInformation();
    Thread thread = new Thread(new ReceiveInformation());
    int count;
    Protocol protocol = new Protocol();
    BackClient backClient = new BackClient();
    private JPanel contentPane;

    public FriendListUi() {
        information.addThisWindows("friendlist", this);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 228, 433);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        final JLabel lblNewLabel = new JLabel(" 好友列表");
        lblNewLabel.setIcon(new ImageIcon("xia.png"));
        lblNewLabel.setBounds(10, 10, 192, 15);
        contentPane.add(lblNewLabel);
        JButton button = new JButton("添加好友");
        button.addActionListener(arg0 -> {
            AddFriendUI addFriendUI = new AddFriendUI();
            addFriendUI.getNickname(nickname);
            addFriendUI.setVisible(true);
        });
        button.setBounds(10, 359, 95, 25);
        contentPane.add(button);
        thread.start();

        lblNewLabel.addMouseListener(new MouseAdapter() {
            int isDrag = 1;

            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 1) {
                    if (isDrag % 2 == 0) {
                        lblNewLabel.setIcon(new ImageIcon("xia.png"));
                        for (int i = 0; i < friendID.length; i++) {
                            list[i].setVisible(true);
                        }
                    }
                    if (isDrag % 2 == 1) {
                        lblNewLabel.setIcon(new ImageIcon("you.png"));
                        for (int i = 0; i < friendID.length; i++) {
                            list[i].setVisible(false);
                        }
                    }
                    isDrag++;
                }
            }
        });
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
        String offlineMessage = protocol.getOfflineMassage(nickname);
        backClient.SendMassage(offlineMessage);
        String okMessage = protocol.backOfflineMassageOk(nickname);
        backClient.SendMassage(okMessage);
    }

    public void setFriendID(String friendNickname) {
        this.friendNickname = friendNickname;
        friendID = friendNickname.split(",");

        for (int i = 0; i < friendID.length; i++) {
            list[i] = new JLabel(friendID[i]);
            final String aid = friendID[i];
            list[i].setBounds(20, 30 * (i + 1), 182, 22);
            contentPane.add(list[i]);
            list[i].addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    if (e.getClickCount() == 2) {
                        UserUI ui = new UserUI();
                        ui.getFriendID(aid);
                        ui.setNickname(nickname);
                        ui.setVisible(true);
                    }
                }
            });
        }
        count = friendID.length;
    }

    public void setNewFriend(String friendID) {
        list[count] = new JLabel(friendID);
        list[count].setBounds(20, 30 * (count + 1), 182, 22);
        contentPane.add(list[count]);
        final String aid = friendID;
        list[count].addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    UserUI ui = new UserUI();
                    ui.getFriendID(aid);
                    ui.setNickname(nickname);
                    ui.setVisible(true);
                }
            }
        });
        contentPane.repaint();
    }
}

